using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using NUnit.Framework;

namespace DatabaseUnitTesting.Utilities.StoredProcedure.Tests
{
    [TestFixture]
    public class StoredProcedureParserTests
    {
        [Test]
        public void StoredProcedureParserTest()
        {
            string connectionString =
                "Application Name=UnitTests;Trusted_Connection=true;Server=(local);Database=astyler";

            SqlConnection connection = new SqlConnection(connectionString);
            connection.Open();

            SqlCommand storedProcedure = new SqlCommand("TwoResultSets", connection);
            storedProcedure.CommandType = CommandType.StoredProcedure;
            storedProcedure.Parameters.AddWithValue("@rows", "3000");

            Database results = ResultSetParser.Parse(storedProcedure);
            Assert.AreEqual(1, results.TableCount);

            IEnumerator<KeyValuePair<Table, int>> tableEnum = results.Tables.GetEnumerator();
            tableEnum.MoveNext();

            Assert.AreEqual(3000, tableEnum.Current.Key.RowCount);
            Assert.AreEqual(3, tableEnum.Current.Key.FieldCount);

            string schema = tableEnum.Current.Key.Schema.KeyString;
            string[] columns = schema.Split(new string[] { Row.DELIMITER }, StringSplitOptions.None);

            string[] parts1 = columns[1].Split(new string[] { Column.DELIMITER }, StringSplitOptions.None);
            string[] parts2 = columns[2].Split(new string[] { Column.DELIMITER }, StringSplitOptions.None);
            string[] parts3 = columns[3].Split(new string[] { Column.DELIMITER }, StringSplitOptions.None);

            Assert.AreEqual("id", parts1[0]);
            Assert.AreEqual("int", parts1[1]);

            Assert.AreEqual("c", parts2[0]);
            Assert.AreEqual("varchar(100)", parts2[1]);

            Assert.AreEqual("price", parts3[0]);
            Assert.AreEqual("decimal(18,0)", parts3[1]);
        }

        [Test]
        public void EqualityTest()
        {
            string database = "astyler";
            string server = "(local)";
            string connectionString = "Application Name=UnitTests;Trusted_Connection=true;Server=" + server +
                                      ";Database=" + database;

            SqlConnection connection = new SqlConnection(connectionString);
            connection.Open();

            SqlCommand storedProcedure = new SqlCommand("TwoResultSets", connection);
            storedProcedure.CommandType = CommandType.StoredProcedure;
            storedProcedure.Parameters.AddWithValue("@rows", "3000");

            Database one = ResultSetParser.Parse(storedProcedure);

            storedProcedure.Parameters["@rows"].Value = 2999;
            Database two = ResultSetParser.Parse(storedProcedure);

            Assert.IsTrue(one.Equals(one));
            Assert.IsTrue(two.Equals(two));
            Assert.IsFalse(one.Equals(two));
            Assert.IsFalse(two.Equals(one));
        }
    }
}